﻿using CrawlerFramework.Models;
using System.Linq;
using GenericRepository.EF;

namespace CrawlerFramework.Repository
{
    public class LinkRepository:BaseCrawlerRepository<Link>
    {
        public void InsertOrUpdate(Link entity)
        {
            var check = this.Single(l => (l.Url == entity.Url && !entity.IsDelete) || l.LinkId == entity.LinkId);
            if (check == null)
                base.Add(entity);
            else
            {
                if (entity.LinkId == 0)
                {
                    entity.LinkId = check.LinkId;
                    this.Context.Entry(check).State = System.Data.Entity.EntityState.Detached;
                }
                base.Edit(entity);
            }
                
        }
    }
}
